(function () {
  new Vue({
    el: '#distribution',
    data() {
      return {
        projectList: [],
        sortTableTitle: [],
        isEdit:false,
        projectId:'',
        projectName:'',
        proExpenditure:[],
        configObj:'',
      }
    },
    mounted() {
      hlSelectUi(true)
      this.getProject()
      this.getProjectCost()
    },
    methods: {
      // 拖拽效果
      initSortable() {
        const self = this;
        const el = document.getElementById('sortableTable');
        Sortable.create(el, {
          draggable: 'ul',
          onEnd(evt) {
            const beginIndex = evt.oldIndex;
            const endIndex = evt.newIndex;
            const newArr = JSON.parse(JSON.stringify(self.sortTableTitle));
            newArr.splice(endIndex, 0, newArr.splice(beginIndex, 1)[0])
            self.sortTableTitle = [];
            self.$nextTick(() => {
              self.sortTableTitle = newArr
            })
          }
        })
      },
      // 获取所有项目
      getProject() {
        hlhttp.get({
          url: 'user/projects',
          success: (res) => {
            res = hlArrayUtil.removeRepeat(res, 'projectId')
            this.projectList = res;
          }
        })
      },
      //选择单个项目
      getprojectId(id){
        this.projectId = id
        this.getProjectCost()
      },
      // 获取所有项目的流水分配
      getProjectCost() {
        hlhttp.get({
          url: 'project/expenditure_configs',
          data:{
            project_id:this.projectId || '#'
          },
          success: (res) => {
            this.proExpenditure = res
          }
        })
      },
      //点击配置按钮
      configBut(id,name){
        this.projectName = name
        hlhttp.get({
          url: 'project/expenditure_config',
          data:{
            project_id:id
          },
          success: (res) => {
            this.sortTableTitle = res.expenditures
            this.configObj = res
            this.isEdit = true
            this.initSortable();
          }
        })
      },
      // 确认更改顺序
      sureChange(){
        let ids = []
        let idc = ''
        if(!this.sortTableTitle.length){
          return
        }
        this.sortTableTitle.some(item =>{
          if(item.configLevel == 'C'){
            idc = item.id
          }
        })
        this.sortTableTitle.map(item =>{
          ids.push(item.id)
        })
        hlhttp.put({
          url: 'project/expenditure_config',
          data:{
            id:idc,
            project_id:this.configObj.projectId,
            expenditure_config_ids:ids.join(',')
          },
          success: (res) => {
            this.sortTableTitle = res.expenditures
            this.isEdit = false
            this.getProjectCost();
          }
        })
      }
    },
  })
}())
